
            package com.example.task4.dao;

            import com.example.task4.domain.User;
            import com.example.task4.util.DataBaseUtil;

            import java.sql.Connection;
            import java.sql.PreparedStatement;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;

            public class UserDao {

            public boolean save(User user) {
                String sql = "INSERT INTO users(username,password,email,avatar) VALUES(?,?,?,?)";
                try (Connection con = DataBaseUtil.getConnection();
                     PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                    ps.setString(1, user.getUsername());
                    ps.setString(2, user.getPassword());
                    ps.setString(3, user.getEmail());
                    ps.setString(4, user.getAvatarUrl()); // Add avatar URL
                    int affected = ps.executeUpdate();
                    if (affected > 0) {
                        try (ResultSet keys = ps.getGeneratedKeys()) {
                            if (keys.next()) {
                                user.setId(keys.getInt(1)); // 设置生成的 user_id
                            }
                        }
                        return true;
                    } else {
                        return false;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    return false;
                }
            }


                public User findByUsername(String username) {
                    String sql = "SELECT user_id,username,password,email,avatar FROM users WHERE username=?";
                    try (Connection con = DataBaseUtil.getConnection();
                         PreparedStatement ps = con.prepareStatement(sql)) {
                        ps.setString(1, username);
                        ResultSet rs = ps.executeQuery();
                        if (rs.next()) {
                            User u = new User();
                            u.setId(rs.getInt("user_id"));
                            u.setUsername(rs.getString("username"));
                            u.setPassword(rs.getString("password"));
                            u.setEmail(rs.getString("email"));
                            u.setAvatarUrl(rs.getString("avatar")); // Add avatar URL
                            return u;
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                }


                public User findByUsernamePassword(String username, String password) {
                        String sql = "SELECT user_id,username,password,email,avatar FROM users WHERE username=? AND password=?";
                        try (Connection con = DataBaseUtil.getConnection();
                             PreparedStatement ps = con.prepareStatement(sql)) {
                            ps.setString(1, username);
                            ps.setString(2, password);
                            ResultSet rs = ps.executeQuery();
                            if (rs.next()) {
                                User u = new User();
                                u.setId(rs.getInt("user_id"));
                                u.setUsername(rs.getString("username"));
                                u.setPassword(rs.getString("password"));
                                u.setEmail(rs.getString("email"));
                                u.setAvatarUrl(rs.getString("avatar")); // Add avatar URL
                                return u;
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

            }